package com.android.openapp.ui;

import java.util.List;


import data.LocationViewCache;
import data.Place;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import android.widget.RelativeLayout;
import android.widget.TextView;

public class LocationAdapter extends ArrayAdapter<Place>{
	private int resource;
    private LayoutInflater  inflater;

    public LocationAdapter( Context context, int resourceId, List<Place> objects ) {
            super( context, resourceId, objects );
            resource = resourceId;
            inflater = LayoutInflater.from( context );
    }
    
    @Override
    public View getView ( int position, View convertView, ViewGroup parent ) {

            // Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
            Place loc = getItem( position );

            LocationViewCache viewCache;

            if ( convertView == null ) {
                    convertView = ( RelativeLayout ) inflater.inflate( resource, null );
                    viewCache = new LocationViewCache( convertView );
                    convertView.setTag( viewCache );
            }
            else {
                    convertView = ( RelativeLayout ) convertView;
                    viewCache = ( LocationViewCache ) convertView.getTag();
            }

            // Prendiamo le view dalla cache e mettiamoci i valori
            //ImageView ivPhoto = viewCache.getImageViewPhoto();
            //ivPhoto.setImageResource( Loca.getPhotoRes() );

            TextView tvName = viewCache.getTextViewName();
            Log.i("LocationAdapter",loc.getDenominazione());
            tvName.setText( loc.getDenominazione() );

            TextView tvTip = viewCache.getTextViewTipologia();
            tvTip.setText( loc.getTipologia() );

            return convertView;

    }
}
